#include "common.h"
#include "basic_op.h"
#include "keyschedule.h"

#include <stdlib.h>

void keyschedule( word user_key[] , const int n , nibble roundkey[][8] )    //user_key[1] is the higher word , user_key[0] is the lower word
{
	word key_state[2] = { 0 } ;
	int i = 0 ; 
	int j = 0 ;
	for ( i = 0 ; i < n ; ++i )
	{
		key_state[i] = user_key[i] ;
	}
	for ( i = 0 ; i < MIBS_ROUND ; ++i )
	{
		key_state[1] = user_key[1] ;
		user_key[1] = (user_key[1]>>15) + (user_key[0]<<17) ;
		user_key[0] = (user_key[0]>>15) + (key_state[1]<<17) ;
		user_key[1] = (S_BOX[(user_key[1]>>28)&0x0f]<<28) + (user_key[1]&0xfffffff) ;
		user_key[0] = user_key[0] ^ ((i+1)*0x800) ;
		for ( j = 0 ; j < NIBBLE_NUM/2 ; ++j )
		{
			roundkey[i][7-j] = (user_key[1]>>(4*j))&0x0f ;
		}
		printf( "\n%x" , user_key[1] ) ;
	}
}